﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookMS
{
    internal class Dao
    {
        SqlConnection sc;
        // 从配置文件获取数据库连接字符串
        private readonly string _connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        public SqlConnection connect()
        {
            string str = _connectionString;//数据库连接字符串

            sc=new SqlConnection(str);//创建数据库连接对象
            sc.Open();//打开数据库
            return sc;
        }


        public SqlCommand command(string sql)
        {
            SqlCommand cmd=new SqlCommand(sql,connect());
            return cmd;

        }

        public int Execute(String sql)//更新操作
        {
            return command(sql).ExecuteNonQuery();
        }


        public SqlDataReader read(String sql) //读取操作
        {
            return command(sql).ExecuteReader();
        }

        public void Daoclose()//关闭数据库连接
        {
            sc.Close();
        }
    }
}
